package com.flowerplus.store.common.execption.utils;

import android.database.sqlite.SQLiteException;
import android.os.FileUriExposedException;

import com.flowerplus.store.App;
import com.flowerplus.store.Logger;
import com.flowerplus.store.R;
import com.flowerplus.store.common.data.utils.ui.ResourcesUtil;
import com.flowerplus.store.common.execption.ApiException;

import org.greenrobot.greendao.DaoException;

import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.ConcurrentModificationException;

import retrofit2.HttpException;

import static com.flowerplus.store.common.data.http.utils.HttpCodeUtil.byCodeShowErrorMsg;


/**
 * Created by ems on 2017/3/3.
 * 异常处理类
 */

public class ExceptionHandle {
    private static ResourcesUtil mResourcesUtil = new ResourcesUtil(App.getApp().getApplicationContext());

    private ExceptionHandle() {
        //no instance
    }

    public static String handle(Throwable e) {
        Logger.e("ExceptionHandle", e.getMessage(), e);
        e.printStackTrace();
        if (e instanceof HttpException) {
            int code = ((HttpException) e).code();
            String msg = byCodeShowErrorMsg(code);
            return msg;
        } else if (e instanceof NullPointerException) {
            return mResourcesUtil.getString(R.string.error_nullpointer_msg);
        } else if (e instanceof ConnectException) {
            return mResourcesUtil.getString(R.string.error_connectexception_msg);
        } else if (e instanceof SocketTimeoutException) {
            return mResourcesUtil.getString(R.string.error_socket_timeout_msg);
        } else if (e instanceof ClassCastException) {
            return mResourcesUtil.getString(R.string.error_classcast_msg);
        } else if (e instanceof DaoException) {
            return mResourcesUtil.getString(R.string.error_dao_msg);
        } else if (e instanceof IOException) {
            return mResourcesUtil.getString(R.string.error_io_msg);
        } else if (e instanceof ArithmeticException) {
            return mResourcesUtil.getString(R.string.error_compress_msg);
        } else if (e instanceof ApiException) {
            return e.getMessage();
        } else if (e instanceof UnsupportedOperationException) {
            return mResourcesUtil.getString(R.string.error_uri_msg);
        } else if (e instanceof SQLiteException) {
            return mResourcesUtil.getString(R.string.error_sqlite_msg);
        } else if (e instanceof IndexOutOfBoundsException) {
            return mResourcesUtil.getString(R.string.error_indexof_msg);
        } else if (e instanceof ConcurrentModificationException) {
            return mResourcesUtil.getString(R.string.error_cme_msg);
        } else if (e instanceof FileUriExposedException) {
            return mResourcesUtil.getString(R.string.error_os7_fileuri_msg);
        }
        return "未知异常";
    }
}